package com.adk.backend.controller;

import com.adk.backend.annotation.RequiresPermission;
import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxPackingList;
import com.adk.backend.service.PackingListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 装箱单列表管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/packing-list")
public class PackingListController {
    
    @Autowired
    private PackingListService packingListService;
    
    /**
     * 分页查询装箱单列表
     */
    @RequiresPermission("list")
    @GetMapping("/list")
    public Result<PageResult<LxPackingList>> getPackingList(
            @RequestParam(required = false) Long orderId,
            @RequestParam(required = false) Integer ctnNo,
            @RequestParam(required = false) String proName,
            @RequestParam(required = false) String sku,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", orderId);
        params.put("ctnNo", ctnNo);
        params.put("proName", proName);
        params.put("sku", sku);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        PageResult<LxPackingList> result = packingListService.getPackingList(params);
        return Result.success(result);
    }
    
    /**
     * 根据订单ID查询装箱单列表
     */
    @RequiresPermission("view")
    @GetMapping("/by-order")
    public Result<List<LxPackingList>> getPackingListByOrderId(@RequestParam Long orderId) {
        List<LxPackingList> result = packingListService.getPackingListByOrderId(orderId);
        return Result.success(result);
    }
    
    /**
     * 根据ID查询装箱单
     */
    @RequiresPermission("view")
    @GetMapping("/detail")
    public Result<LxPackingList> getPackingDetail(@RequestParam Long id) {
        LxPackingList packing = packingListService.getPackingById(id);
        if (packing != null) {
            return Result.success(packing);
        }
        return Result.error("装箱单不存在");
    }
    
    /**
     * 创建装箱单
     */
    @RequiresPermission("add")
    @PostMapping("/create")
    public Result<LxPackingList> createPacking(@RequestBody LxPackingList packingList) {
        try {
            LxPackingList result = packingListService.createPacking(packingList);
            return Result.success("创建成功", result);
        } catch (Exception e) {
            log.error("创建装箱单失败", e);
            return Result.error("创建装箱单失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量创建装箱单
     */
    @RequiresPermission("add")
    @PostMapping("/batch-create")
    public Result<Boolean> batchCreatePacking(@RequestBody List<LxPackingList> packingList) {
        try {
            boolean success = packingListService.batchCreatePacking(packingList);
            if (success) {
                return Result.success("批量创建成功", true);
            }
            return Result.error("批量创建失败");
        } catch (Exception e) {
            log.error("批量创建装箱单失败", e);
            return Result.error("批量创建装箱单失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新装箱单
     */
    @RequiresPermission("edit")
    @PostMapping("/update")
    public Result<Boolean> updatePacking(@RequestBody LxPackingList packingList) {
        try {
            boolean success = packingListService.updatePacking(packingList);
            if (success) {
                return Result.success("更新成功", true);
            }
            return Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新装箱单失败", e);
            return Result.error("更新装箱单失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量更新装箱单
     */
    @RequiresPermission("edit")
    @PostMapping("/batch-update")
    public Result<Boolean> batchUpdatePacking(@RequestBody List<LxPackingList> packingList) {
        try {
            boolean success = packingListService.batchUpdatePacking(packingList);
            if (success) {
                return Result.success("批量更新成功", true);
            }
            return Result.error("批量更新失败");
        } catch (Exception e) {
            log.error("批量更新装箱单失败", e);
            return Result.error("批量更新装箱单失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除装箱单
     */
    @RequiresPermission("del")
    @PostMapping("/delete")
    public Result<Boolean> deletePacking(@RequestParam Long id) {
        try {
            boolean success = packingListService.deletePacking(id);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除装箱单失败", e);
            return Result.error("删除装箱单失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据订单ID删除装箱单
     */
    @RequiresPermission("del")
    @PostMapping("/delete-by-order")
    public Result<Boolean> deletePackingByOrderId(@RequestParam Long orderId) {
        try {
            boolean success = packingListService.deletePackingByOrderId(orderId);
            if (success) {
                return Result.success("删除成功", true);
            }
            return Result.error("删除失败");
        } catch (Exception e) {
            log.error("根据订单ID删除装箱单失败", e);
            return Result.error("根据订单ID删除装箱单失败：" + e.getMessage());
        }
    }
}

